#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Paul F. Idstein.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.db import polymodel
from django.utils import simplejson

class Wave(polymodel.PolyModel):
	waveID = db.TextProperty()

class Blip(Wave):
	blipID = db.TextProperty()
	creator = db.UserProperty()
	version = db.IntegerProperty()
	contributors = db.ListProperty(users.User)
	elements = db.TextProperty()
	annotations = db.TextProperty()
	lastModifiedTime = db.DateTimeProperty()
	content = db.TextProperty()

class Wavelet(Wave):
	waveletID = db.TextProperty()
	creator = db.UserProperty()
	creationTime = db.DateTimeProperty(auto_now=True)
	domain = db.TextProperty()
	lastModifiedTime = db.DateTimeProperty()
	title = db.TextProperty()
	dataDocuments = db.TextProperty()
	participants = db.ListProperty(users.User)
	tags = db.TextProperty()
	rootBlip = db.ReferenceProperty(Blip)
	blips = db.ListProperty(db.Key)

class UserSettings(db.Model):
	"""Model for a user's settings"""
	user = db.UserProperty()
	creationTime = db.DateTimeProperty(auto_now=True)
	lastModifiedTime = db.DateTimeProperty()
	version = db.IntegerProperty()
	settings = db.TextProperty()

	
def constructBlip( parent, blip ):
	result = Blip(parent=parent,
						blipID=blip.blip_id,
						creator=users.User(blip.creator),
						version=blip.version,
						contributors=[users.User(identifier) for identifier in blip.contributors],
#						elements = simplejson.dumps(blip.elements.values()),
						annotations = simplejson.dumps(blip.annotations.serialize()),
#						lastModifiedTime = datetime.fromtimestamp(blip.last_modified_time),
						content = blip.text)
	result.put()
	for child in blip.child_blips:
		constructBlip( result, child )
	return result